<script setup>
import reviewItem from '@/components/reviewItem.vue'
import { nyr } from '@/utils/tool'
import { recoverReviewAPI } from '@/apis/home'
import { ref } from 'vue';
import useDataStore from '../store';
import { storeToRefs } from 'pinia';
import { useRoute } from 'vue-router';



defineOptions({
    name: 'reviewItem'
})

const dataStore = useDataStore()
const { userInfo } = storeToRefs(dataStore)

const route = useRoute()
let { article_id } = route.query

let emits = defineEmits(['getReviewList'])
let props = defineProps(['reviewList', 'getReviewList', 'article_author_id', 'level'])

/* ---------------------------------- 回复评论 ---------------------------------- */
let review_id = ref()      // 回复哪条的评论id
let sender_id = ref()         // 这条评论是哪个用户发的
let drawer = ref(false)  // 是否展示Drawer
let recover = ref()     // 回复评论内容
// 点击外部的回复按钮打开回复框
const recoverAction = (id, user_id) => {
    review_id.value = id
    sender_id.value = user_id
    drawer.value = true

}
// 输入完内容后点击回复按钮
const recoverPostAction = async () => {
    let data = await recoverReviewAPI(recover.value, review_id.value, userInfo.value, article_id, sender_id.value)
    if (data.code === 200) {
        ElMessage({
            message: '回复成功',
            type: 'success'
        })
    }
    // props.getReviewList()
    emits('getReviewList')
    drawer.value = false
}


</script>

<template>
    <div class="review-content">
        <el-drawer :with-header="false" custom-class="drawer" direction="btt" v-model="drawer"
            :before-close="handleClose">
            <div class="drawer-content">
                <el-input rows="4" type="textarea" v-model="recover" placeholder="在这里回复评论" />
                <el-button class="elbtn" color="#8e8cd8" plain @click="recoverPostAction">回复</el-button>
            </div>

        </el-drawer>
        <div class="item" v-for="item in props.reviewList" :key="item.review_id">
            <div class="item-user">
                <div class="user-img"><img :src="item.user_avatar" alt=""></div>
                <div class="user-name">{{ item.user_name }}</div>
                <div class="user-time">{{ nyr(new Date(item.review_time * 1), '-') }}</div>
            </div>
            <div class="item-text" @click="recoverAction(item.review_id, item.user_id)">{{ item.review_content }}</div>
            <!-- <div class="item-answer">回复{{ props.level }}</div> -->
            <div class="item-answer-content">
                <div class="answer-item" v-for="answer in item.childrenList">
                    <div class="item-user">
                        <div class="user-img"><img :src="answer.user_avatar" alt=""></div>
                        <div class="user-name">{{ answer.user_name }}</div>
                        <div class="user-time">{{ nyr(new Date(answer.review_time * 1), '-') }}</div>
                    </div>
                    <div class="item-text" @click="recoverAction(asnwer.review_id, answer.user_id)"><span>回复：@{{
                        answer.extend.user_name }} </span> {{ answer.review_content }}
                    </div>
                </div>
                <!-- <reviewItem v-if="reviewList.length" :reviewList="item.childrenList" :level="Number(props.level + 1)">
                </reviewItem> -->
            </div>
        </div>
    </div>
</template>

<style scoped lang='scss'>
.review-content {
    // padding-left: 30px;
    box-sizing: border-box;
    margin-top: 0px;
    width: 100%;
    position: relative;

    .drawer-content {
        .elbtn {
            margin-top: 20px;
        }
    }

    .item {
        margin-top: 20px;
        width: 100%;
        // border-left: #eee solid 1px;
        padding-bottom: 30px;
        overflow: hidden;

        .item-user {
            width: 100%;
            display: flex;
            align-items: center;

            .user-img {
                border-radius: 50%;
                overflow: hidden;
                width: 40px;
                // height: 40px;

                img {
                    width: 100%;
                    height: 100%;
                }
            }

            .user-name {
                // width: 80%;
                flex: 1;
                font-size: 16px;
                margin-left: 20px;
            }
        }

        .item-text {
            cursor: pointer;
            width: 100%;
            margin-top: 10px;
            box-sizing: border-box;
            background-color: #eee;
            border-radius: 5px;
            padding: 10px;
        }

        .item-answer {
            cursor: pointer;
            display: inline-block;
            box-sizing: border-box;
            padding: 5px 0px;
            margin-top: 10px;
            margin-left: 7px;
            transition: all .25s ease;

            // background-color: #ddd;
            &:hover {
                color: #8e8cd8;
            }


        }

        .item-answer-content {
            // width: 100%;
            margin-top: 10px;
            margin-left: 40px;
            box-sizing: border-box;

            .answer-item {
                // width: 100%;
                margin: 20px 0px;

                .item-user {
                    width: 100%;
                    display: flex;
                    align-items: center;

                    .user-img {
                        border-radius: 50%;
                        overflow: hidden;
                        width: 40px;
                        // height: 40px;

                        img {
                            width: 100%;
                            height: 100%;
                        }
                    }

                    .user-name {
                        // width: 80%;
                        flex: 1;
                        font-size: 16px;
                        margin-left: 20px;
                    }
                }

                .item-text {
                    width: 100%;
                    margin-top: 10px;
                    box-sizing: border-box;
                    background-color: #eee;
                    border-radius: 5px;
                    padding: 10px;
                }
            }
        }
    }
}
</style>